<template>
  <div>
    <Breadcrumb :opt-data="attributeOptData"></Breadcrumb>
    <PageLayout>
      <div class="table-container height-100">
        <EZTable
          v-loading="tableLoading"
          :table-id="tableId"
          :table-hdl="tableHdl"
          :rows="attributeData"
          :custom-empty="true"
          @pageChanged="getAttributeData"
          @sizeChanged="getAttributeData"
          @filterChange="getAttributeData">
          <EmptyContent slot="empty" :opt-data="attributeOptData"></EmptyContent>
        </EZTable>
      </div>
    </PageLayout>
  </div>
</template>
<script>
  import {tableIdConfig, tableOptBtnConfig} from '@/config/table';
  import {msgCode} from "@/config/string";
  import {confirmHandle, deepCopyWithJson, JSONParse, messageHandle} from "@/utils";
  import {assembleTableOptBtn, getTableConfig, TableStateHdl} from "@/modules/routeParamStateHdl";
  import attributeTableCfg from "../../config/table/attribute";
  import EZTable from '../../components/table/EZTable';
  import {attributeOriginType, entityType_asset} from "@/config/common";
  import {routerMeta} from "@/router/routerMeta";
  export default {
    name: "AttributeManagement",
    components: {EZTable},
    data() {
      return {
        tableLoading: true,
        tableId: tableIdConfig.attribute,
        tableHdl: null,
        attributeData: [],
        entityType_asset,
        attributeOriginType,
      }
    },
    computed: {
      attributeOptData() {
        return [
          {
            type: 'btn',
            label: '新增属性',
            icon: 'icon-tianjia',
            callback: () => {
              this.clickCreateBtn();
            }
          }
        ];
      }
    },
    activated() {
      this.initTableConfig();
      this.getAttributeData();
    },
    methods: {
      initTableConfig() {
        if (this.tableHdl) return;
        const {columns = [], filters = []} = getTableConfig(this.tableId, attributeTableCfg);
        assembleTableOptBtn(columns, ({type = '', data = {}}) => {
          if (!type) return;
          const deviceType = deepCopyWithJson(data);
          switch (type) {
            case tableOptBtnConfig.detail.type:
              this.$router.push({
                name: routerMeta.attributeDetail.name,
                params: {
                  id:data._id,
                  type:'ATTRIBUTE'
                },
              });
              break;
            case tableOptBtnConfig.edit.type:
              this.$router.push({
                name: routerMeta.attributeEdit.name,
                params: {
                  id:data._id,
                  type:'ATTRIBUTE'
                },
              });
              break;
            case tableOptBtnConfig.delete.type:
              this.deleteAttributeType(deviceType);
              break;
            default:
              break;
          }
        });
        this.tableHdl = new TableStateHdl(this.$route.name, this.tableId, columns, filters);
      },
      clickCreateBtn() {
        this.$router.push({
          name: routerMeta.attributeAdd.name,
          params: {
            type:'ATTRIBUTE'
          }
        });
      },
      getAttributeData() {
        this.tableLoading = true;
        const selector = {type: "ATTRIBUTE"};
        const tableParam = this.tableHdl.assembleTableParam();
        API.platformMessage.platformMessageList({...tableParam, selector}).then(result => {
          this.tableLoading = false;
          const {data = [], total = 0} = result;
          this.tableHdl.total = total;
            this.attributeData = data.map(d => {
              const attributeType =  this.attributeOriginType.find(type => type.value === d.attributeOrigin) || {};
              const entityType = this.entityType_asset.find(entityType => entityType.value === d.entityType) || {};
              return {
                ...d,
                attributeTypeText: attributeType.label || '--',
                entityTypeText: entityType.label || '--',
              }
            });
        }).catch(err => {
          console.log('err', err)
          this.tableLoading = false;
        });
      },
      deleteAttributeType(deviceType = {}) {
        confirmHandle({label: `确定要删除属性：${deviceType.name} 吗?`}).then(() => {
          API.platformMessage.platformMessageDelete({_id: deviceType._id}).then(result => {
            messageHandle({code: msgCode.DELETE_SUCCESS, title: '属性'});
            this.tableHdl.judgeTableMatched();
            this.getAttributeData();
          });
        }).catch(() => {
        });
      },
    },

  }
</script>


<style scoped lang="less">

</style>
